//
//  BaseRequest.m
//  忘不了
//
//  Created by rimi on 16/7/8.
//  Copyright © 2016年 handuan. All rights reserved.
//

#import "BaseRequest.h"
#import <AVOSCloud/AVOSCloud.h>
#import "UserModel.h"
@implementation BaseRequest
//注册
+ (void)startRegisterWithUserName:(NSString *)userName password:(NSString *)password email:(NSString *)email completionhanlder:(RequestCompletion)callBack{
    Response *response = [[Response alloc]init];
    UserModel *user = [UserModel shareUserModel];
    //判断用户表里是否存在同样的用户名
    AVQuery *query = [AVQuery queryWithClassName:@"DefaultUser"];
    [query whereKey:@"userNmae" equalTo:userName];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        NSArray<AVObject *> *priorityEqualsZeroTodos = objects;
        if (priorityEqualsZeroTodos.count == 0) {
            //如果不存在相同用户名
            //判断是否存在相同邮箱
            AVQuery *query = [AVQuery queryWithClassName:@"DefaultUser"];
            [query whereKey:@"email" equalTo:email];
            [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                NSArray<AVObject *> *priorityEqualsZeroTodos = objects;
                //如果存在相同邮箱
                if (priorityEqualsZeroTodos.count > 0) {
                    NSLog(@"邮箱已存在");
                    response.isSuccess = NO;
                    response.resultObject = @"邮箱已存在";
                    callBack(response);

                }else{
                    //如果不存在相同邮箱
                    NSLog(@"可以注册");
                    AVObject *todo = [AVObject objectWithClassName:@"DefaultUser"];
                    [todo setObject:userName forKey:@"userNmae"];
                    [todo setObject:password forKey:@"password"];
                    [todo setObject:email forKey:@"email"];
                    [todo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                        if (succeeded) {
                            // 存储成功
                            NSLog(@"%@",todo.objectId);// 保存成功之后，objectId 会自动从云端加载到本地
                            user.objectId = todo.objectId;
                            response.isSuccess = YES;
                            response.resultObject = @"";
                        } else {
                            NSLog(@"错误原因：%@",error.localizedDescription);
                            response.isSuccess = NO;
                            response.resultObject = @"账号已存在";
                        }
                        callBack(response);

                    }];
                    
                }
//                callBack(response);

            }];
        }else{
            //如果存在
            response.isSuccess = NO;
            response.resultObject = @"账号已存在";
            callBack(response);

        }
//        callBack(response);
    }];
    
}
//登录
+ (void)startLoginWithUserName:(NSString *)userName password:(NSString *)password completionhanlder:(RequestCompletion)callBack{
//    return;
    Response *response = [[Response alloc]init];
    UserModel *user = [UserModel shareUserModel];
    AVQuery *checkUserName = [AVQuery queryWithClassName:@"DefaultUser"];
    [checkUserName whereKey:@"userNmae" equalTo:userName];
    AVQuery *checkPassword = [AVQuery queryWithClassName:@"DefaultUser"];
    [checkPassword whereKey:@"password" equalTo:password];
    AVQuery *query = [AVQuery andQueryWithSubqueries:[NSArray arrayWithObjects:checkUserName,checkPassword,nil]];
    [query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
        NSLog(@"%@",results);
        if (results.count != 0) {
            user.userNmae = userName;
            user.password = password;
            user.objectId = results[0][@"objectId"];
            response.isSuccess = YES;
        }else{
            user.userNmae = @"";
            user.password = @"";
            response.isSuccess = NO;
        }
        callBack(response);
    }];

}
//修改密码
+ (void)startModifyPasswordWithNewPassword:(NSString *)newPassword completionhanlder:(RequestCompletion)callBack{
    Response *response = [[Response alloc]init];
    UserModel *user = [UserModel shareUserModel];
    AVQuery *query = [AVQuery queryWithClassName:@"DefaultUser"];
    [query whereKey:@"userNmae" equalTo:user.userNmae];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (objects.count > 0) {
            NSDictionary *dic = objects[0];
            AVObject *todo =[AVObject objectWithClassName:@"DefaultUser" objectId:dic[@"objectId"]];
            [todo setObject:newPassword forKey:@"password"];
            [todo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    NSLog(@"修改成功");
                    response.isSuccess = YES;
                }else{
                    NSLog(@"修改失败");
                    response.isSuccess = NO;
                }
                callBack(response);
            }];
            
        }
    }];
    
}
//忘记密码
+ (void)startFindPasswordWithEmail:(NSString *)email completionhanlder:(RequestCompletion)callBack{
    Response *response = [[Response alloc]init];
    AVQuery *query = [AVQuery queryWithClassName:@"DefaultUser"];
    [query whereKey:@"email" equalTo:email];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (objects.count > 0) {
            NSDictionary *dic = objects[0];
            //如果找到邮箱
            response.resultObject = dic[@"password"];
            NSLog(@"%@",response.resultObject);
            response.isSuccess = YES;
        }else{
            response.resultObject = @"";
            response.isSuccess = NO;
        }
        callBack(response);
    }];
}
//生活小提示
+ (void)startGetLifeKeyWithcompletionhanlder:(RequestCompletion)callBack{
    Response *response = [[Response alloc]init];
    AVQuery *query = [AVQuery queryWithClassName:@"LifeKey"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (objects.count > 0) {
            NSArray<AVObject *> *todos = objects;
            response.isSuccess = YES;
            response.resultObject = todos;
        }else{
            response.isSuccess = NO;
            response.resultObject = nil;
        }
        callBack(response);
    }];

}
+ (void)startGetNoticeWithcompletionhanlder:(RequestCompletion)callBack{
    Response *response = [[Response alloc]init];
    AVQuery *query = [AVQuery queryWithClassName:@"Notice"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (objects.count > 0) {
            NSArray<AVObject *> *todos = objects;
            response.isSuccess = YES;
            response.resultObject = todos;
        }else{
            response.isSuccess = NO;
            response.resultObject = nil;
        }
        callBack(response);
    }];
}

@end
